using Castle.MonoRail.Framework;
using WineCellar.Web;
using WineCellar.Web.Controllers;

namespace WineCellar.Web.Controllers
{
    [ControllerDetails("Login")]
    [Layout("index")]
    public class LoginController : MessageControllerBase<LoginController.PropertyAccessorImpl>
    {
        public class PropertyAccessorImpl : PropertyAccessorBase
        {
            public class Fields
            {
                public const string ReturnUrl = "returnUrl";
            }
        }

        public void AjaxShow(string returnUrl)
        {
            CancelLayout();
            RenderView("Show");         
        }
        public void Show(string returnUrl)
        {
            
        }
        public void Login(string username, string password, string returnUrl)
        {
            if (!SecurityHelper.Authenticate(this, username, password, false, returnUrl))
            {
                this.PropertyAccessor.AddMessage(new Message("Incorrect username of password. Please try again.", Message.Enums.MessageClass.Warning));
                RedirectToAction("Show", string.Format("returnUrl={0}", returnUrl));
            }
            else
            {
                string[] splitUrl = returnUrl.Split("/".ToCharArray());
               Redirect(splitUrl[0],splitUrl[1]);   
            }            
        }
        public void Logout()
        {
            SecurityHelper.Logout(this.Context);
        }

    }
}
